package com.b2b.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.SystemAdministratorDto;
import com.b2b.model.SystemRoleDto;

import com.b2b.model.entity.system.SystemRole;
import com.b2b.service.SystemAdministratorService;
import com.b2b.service.SystemRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component
public class SystemRoleUtil {


    private final SystemRoleService systemRoleService;
    private final SystemAdministratorService systemAdministratorService;
    @Autowired
    public SystemRoleUtil(SystemRoleService systemRoleService,SystemAdministratorService systemAdministratorService){
        this.systemRoleService = systemRoleService;
        this.systemAdministratorService = systemAdministratorService;
    }
    public int insertSystemRole(SystemRoleDto systemRoleDto){
        SystemRole systemRole = getSystemRole(systemRoleDto);
        SystemRole systemRoleTemp = new SystemRole();
        systemRoleTemp.setRoleName(systemRoleDto.getRoleName());
        int count ;
        if (systemRoleService.selectCount(systemRoleTemp)>=0){
            count = systemRoleService.insert(systemRole);
            if (count <=0){
                return count;
            }else {
                count = systemRole.getRoleId();
                return count;
            }
        }else {
            count = -2;
            return count;
        }
    }

    public List<Integer> getRouteList(Integer roleId){
        SystemRoleDto systemRole = getById(roleId);
        List<Integer> routeList = systemRole.getRoleRouteAuthorityList();
        return routeList;
    }
    public Integer updateSystemAdmin(Integer adminId, Integer roleId){
        SystemAdministratorDto systemAdministratorDto = new SystemAdministratorDto();
        systemAdministratorDto.setAdminId(adminId);
        Integer count = systemAdministratorService.selectCount(systemAdministratorDto);
        if (count <=0 ){
            count = -1;
            return count;
        }else {
            systemAdministratorDto.setRoleId(roleId);
            count = systemAdministratorService.updateByPrimaryKeySelective(systemAdministratorDto);
            return count;
        }
    }
    public List<SystemRoleDto> getAll(){
        List<SystemRole> systemRoles = systemRoleService.selectAll();
        List<SystemRoleDto> systemRoleDtos = new ArrayList<SystemRoleDto>();
        for (int i = 0; i < systemRoles.size(); i++) {
            systemRoleDtos.add(i,getSystemRoleDto(systemRoles.get(i)));
        }
        return systemRoleDtos;
    }

    public SystemRoleDto getById(Integer roleId){
        SystemRole systemRoleTemp = new SystemRole();
        systemRoleTemp.setRoleId(roleId);
        if (systemRoleService.selectCount(systemRoleTemp)<=0){
            throw new CustomUnauthorizedException("该名称不存在(Account not exist.)");
        }
        SystemRole systemRole = systemRoleService.selectByPrimaryKey(roleId);
        SystemRoleDto systemRoleDtoTemp = getSystemRoleDto(systemRole);
        return systemRoleDtoTemp;
    }

    public int updateSystemRole(SystemRoleDto systemRoleDto){
        SystemRole systemRole = new SystemRole();
        systemRole.setRoleId(systemRoleDto.getRoleId());
        int count;
        if (systemRoleService.selectCount(systemRole)<=0){
            count = 2;
        }else {
            count = systemRoleService.updateByPrimaryKeySelective(getSystemRole(systemRoleDto));
        }
        return count;
    }

    public int deleteSystem(SystemRoleDto systemRoleDto){
        SystemRole systemRole = new SystemRole();
        systemRole.setRoleId(systemRoleDto.getRoleId());
        int count;
        if (systemRoleService.selectCount(systemRole)<=0){
            count = 2;
        }else {
            count = systemRoleService.deleteByPrimaryKey(systemRoleDto.getRoleId());
        }
        return count;
    }
    public SystemRole getSystemRole(SystemRoleDto systemRoleDto){
        SystemRole systemRole = new SystemRole();

        String roleRouteAuthorityList = JSON.toJSONString(systemRoleDto.getRoleRouteAuthorityList());
        String roleAPIAuthorityList = JSON.toJSONString(systemRoleDto.getRoleAPIAuthorityList());

        systemRole.setRoleId(systemRoleDto.getRoleId());
        systemRole.setRoleName(systemRoleDto.getRoleName());
        systemRole.setRoleRouteAuthorityList(roleRouteAuthorityList);
        systemRole.setRoleAPIAuthorityList(roleAPIAuthorityList);

        return systemRole;
    }

    public SystemRoleDto getSystemRoleDto(SystemRole systemRole){
        SystemRoleDto systemRoleDto = new SystemRoleDto();

        List<Integer> roleRouteAuthorityList = JSONObject.parseArray(systemRole.getRoleRouteAuthorityList(), Integer.class);
        List<Integer> roleAPIAuthorityList = JSONObject.parseArray(systemRole.getRoleAPIAuthorityList(), Integer.class);

        systemRoleDto.setRoleId(systemRole.getRoleId());
        systemRoleDto.setRoleName(systemRole.getRoleName());
        systemRoleDto.setRoleRouteAuthorityList(roleRouteAuthorityList);
        systemRoleDto.setRoleAPIAuthorityList(roleAPIAuthorityList);

        return systemRoleDto;
    }
}
